<template>
    <div class="login login-container" v-loading.lock="loading">
      <div class="login-form">
        <div class="login-card">
          <div class="login-head">欢迎登录</div>
          <div class="login-body">
            <el-form :model="loginForm" status-icon :rules="loginRule" ref="loginForm" label-width="50px" class="login-ruleForm">
              <el-form-item label="账号" prop="UserAccount">
                <el-input type="text" maxlength="11" v-model="loginForm.UserAccount" @keyup.enter.native="submitForm('loginForm')" placeholder="请输入账号" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="密码" style="margin-bottom:10px;" prop="Password">
                <el-input type="password" v-model="loginForm.Password" @keyup.enter.native="submitForm('loginForm')" placeholder="请输入密码" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item style="margin-bottom:10px;" label-width="20px">
                <el-checkbox v-model="checked">记住我</el-checkbox>
                <a @click="forget" style="float:right;cursor:pointer">忘记密码</a>
              </el-form-item>
              <el-button type="primary" @click="submitForm('loginForm')" style="width:100%">登录</el-button>
            </el-form>
          </div>
        </div>
      </div>

      <el-dialog
        :title="dialogTitle"
        :visible.sync="roomShow"
        width="560px"
        center>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px">
          <el-form-item label="手机号：" prop="Mobile">
            <el-input v-model="ruleForm.Mobile" maxlength="11"></el-input>
          </el-form-item>
          <el-form-item label="验证码：" prop="Code">
            <el-input v-model="ruleForm.Code" style="width:59%;"></el-input>
            <el-button :disabled="!ruleForm.Mobile||ruleForm.Mobile.length!==11||mestime!==60" @click="sendMesBefore" style="width:40%;">{{mestime===60?'获取短信验证码':mestime+'后重发'}}</el-button>
          </el-form-item>
          <el-form-item label="新密码：" prop="Password">
            <el-input v-model="ruleForm.Password" type="password"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="btnSave('ruleForm')">保存</el-button>
          <el-button @click="roomShow = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
</template>
<script type="text/javascript">
export default {
  data (){
    return {
      mestime:60,
      dialogTitle:'',
      roomShow:false,
      ruleForm: {
        Mobile:"",
        Code:"",
        Password:""
      },
      rules: {
        Mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
        ],
        Code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        Password: [
          { required: true,message: '请输入新密码', trigger: 'blur' }
        ]
      },
      checked:false,
      loginForm: {
        UserAccount: '',
        Password: ''
      },
      loginRule: {
        UserAccount: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        Password: [
          { required: true, message: '请输入密码',trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    loading () {
      return this.$store.state.httpconfig.ishttp
    },
  },
  created(){
    let data=JSON.parse(localStorage.getItem('loginRule'));
    let mestime=localStorage.getItem('mestime');
    if(mestime){
      this.mestime=mestime;
      this.sendMes();
    }else{
      this.mestime=60;
    }
    if(data){
      this.loginForm=data;
      this.checked=true;
    }
  },
  methods:{
    sendMesBefore(){
      this.$ajax.SendResetPwdSmsCode({mobile:this.ruleForm.Mobile}).then(res=>{
        if(res.Success){
          this.$message.success(`验证码已发送至${this.ruleForm.Mobile}！`);
          this.sendMes();
        }
      })
    },
    sendMes(){
      if (this.mestime == 0) {
          this.mestime = 60;
      } else {
          this.mestime--;
          localStorage.setItem('mestime',this.mestime);
          setTimeout(()=>{
              this.sendMes()
          },1000)
      }
    },
    forget() {
      this.dialogTitle='忘记密码';
      this.resetForm('ruleForm');
      this.ruleForm={
        Name: "",
        RoleType: 0,
      };
      this.roomShow=true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.checked){
            localStorage.setItem('loginRule',JSON.stringify(this.loginForm))
          }else{
            localStorage.removeItem('loginRule')
          }
          this.$ajax.login(this.loginForm).then((res)=>{
            if(res.Success&&!!res.Data.Hotels.length){
              sessionStorage.setItem('userList',JSON.stringify(res.Data))
              this.$router.push({name:'home'})
            }else{
              this.$message.error('当前用户无权限！');
            }
          })
        } else {
          return false;
        }
      });
    },
    btnSave(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$ajax.ResetPassword(this.ruleForm).then(res=>{
            if(res.Success){
              this.$message.success(this.dialogTitle+'成功!');
              this.$refs[formName].resetFields();
              this.roomShow=false;
            }
          })
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      if(!this.$refs[formName]){return};
      this.$refs[formName].resetFields();
    }
  }
}
</script>
